package com.mrent.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.mrent.entity.AdminPayment;
import com.mrent.entity.AdminShipping;


@Component
public class AdminShippingServiceImpl implements AdminShippingService{
	
	/**
	 * 保存配送方式.
	 * 
	 * @param member
	 */
	public void saveShipping(AdminShipping adminShipping){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.persist(adminShipping);
		}catch(Exception e){
			System.err.println("异常问题");
			e.printStackTrace();
		}
		finally {
			trans.commit();
		}
	}
	
	/**
	 * 修改配送方式.
	 * 
	 * @param member
	 */
	public void updateShipping(AdminShipping adminShipping){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.merge(adminShipping);
		}catch(Exception e){
			System.err.println("异常问题");
			e.printStackTrace();
		}
		finally {
			trans.commit();
		}
	}
	
	/**
	 * 获取配送方式.
	 * 
	 * @param memberId
	 * @return
	 */
	public AdminShipping getShipping(Long shippingId){
		EntityManager em = EMF.getEM();
		AdminShipping shipping = null;
		try{
			shipping = em.find(AdminShipping.class, shippingId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return shipping;
	}
	
	/**
	 * 配送方式列表.
	 * 
	 * @param page
	 * @return
	 */
	public List<AdminShipping> listShipping(Page page){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select from AdminShipping shipping where shipping.enable = 1 ");
		page.setTotalCount(query.getResultList().size());
		query.setFirstResult(page.getFirst());
		query.setMaxResults(page.getPageSize());
		List<AdminShipping> list = query.getResultList();
		return list;
	}

}
